package com.rxt.thermopro.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import com.app.common.base.BaseFragment
import com.app.common.viewmodel.BaseViewModel
import com.rxt.thermopro.R
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.bean.ProfileBean
import com.rxt.thermopro.viewmodel.DeviceStatusViewModel
import com.rxt.thermopro.viewmodel.EventViewModel
import kotlinx.android.synthetic.main.fragment_edit_profile.*
import kotlinx.android.synthetic.main.fragment_edit_profile.backButton
import kotlinx.android.synthetic.main.item_view_set_profile.view.*

class EditProfileFragment : BaseFragment<BaseViewModel>() {
    override fun layoutId(): Int {
        return R.layout.fragment_edit_profile
    }

    companion object {
        private const val ARG_EDIT_TYPE = "typeEdit"
        private const val ARG_PROFILE_DATA = "profileData"
        private const val ARG_DEVICE_POSITION = "devicePosition"
        private const val ARG_ADDRESS = "address"

        const val TYPE_EDIT_TARGET = 1
        const val TYPE_EDIT_RANGE = 2
        const val TYPE_EDIT_CUSTOM = 3

        fun newInstance(sectionType: Int): EditProfileFragment {
            return EditProfileFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_EDIT_TYPE, sectionType)
                }
            }
        }

        fun newInstance(sectionType: Int, profileBean: ProfileBean): EditProfileFragment {
            return EditProfileFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_EDIT_TYPE, sectionType)
                    putParcelable(ARG_PROFILE_DATA, profileBean)
                }
            }
        }

        fun newInstance(
            sectionType: Int,
            profileBean: ProfileBean,
            position: Int,
            address: String
        ): EditProfileFragment {
            return EditProfileFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_EDIT_TYPE, sectionType)
                    putParcelable(ARG_PROFILE_DATA, profileBean)
                    putInt(ARG_DEVICE_POSITION, position)
                    putString(ARG_ADDRESS, address)
                }
            }
        }
    }

    private var currentType = -1
    private lateinit var mProfileBean: ProfileBean
    private lateinit var mEventViewModel: EventViewModel
    private var mPosition = 1
    private var mAddress = ""

    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        mEventViewModel =
            ViewModelProvider(requireActivity()).get(EventViewModel::class.java)
        currentType = arguments?.getInt(ARG_EDIT_TYPE, -1) ?: -1
        mProfileBean = arguments?.getParcelable<ProfileBean>(ARG_PROFILE_DATA) ?: ProfileBean(
            "", "", mutableListOf()
        )
        mPosition = arguments?.getInt(ARG_DEVICE_POSITION) ?: 1
        mAddress = arguments?.getString(ARG_ADDRESS) ?: ""

        currentType = if (mProfileBean.itemValues.isEmpty() && mProfileBean.singleTemp) {
            TYPE_EDIT_TARGET
        } else if (mProfileBean.itemValues.isNotEmpty() && mProfileBean.singleTemp) {
            TYPE_EDIT_CUSTOM
        } else {
            TYPE_EDIT_RANGE
        }

        viewTitle.text = mProfileBean.name
        viewImage.setImageResource(mProfileBean.iconRes)
        if (mProfileBean.isCustomAdd) {
            viewDefault.setText(R.string.text_delete)
        }
        setListener()
    }

    @SuppressLint("SetTextI18n")
    private fun setListener() {
        backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

        when (currentType) {
            TYPE_EDIT_TARGET -> {
                layoutTarget.isVisible = true
                layoutRange.isVisible = false
                layoutCustom.isVisible = false

//                targetNum.text = LocalDataDao.INSTANCE.convertUnit(mProfileBean.highTemperatureNum)

                if (!(LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_F &&
                            mProfileBean.highValueF.isNotEmpty())
                ) {
                    mProfileBean.highValueF =
                        LocalDataDao.INSTANCE.transUnitCToF(mProfileBean.highTemperatureNum)
                    targetNum.text =
                        LocalDataDao.INSTANCE.convertUnit(mProfileBean.highTemperatureNum)
                } else {
                    targetNum.text = mProfileBean.highValueF + LocalDataDao.INSTANCE.getTempUnit()
                }

//                mProfileBean.highValueF =
//                    LocalDataDao.INSTANCE.transUnitCToF(mProfileBean.highTemperatureNum)
                mProfileBean.highValueC = mProfileBean.highTemperatureNum

                buttonSub.setOnClickListener {
                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                        if (targetNum.text.toString().split("°").first().toInt() <= 0
                        ) {
                            return@setOnClickListener
                        }
                    } else {
                        if (targetNum.text.toString().split("°").first().toInt() <= 32
                        ) {
                            return@setOnClickListener
                        }
                    }

                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                        mProfileBean.highTemperatureNum =
                            (mProfileBean.highTemperatureNum.toInt() - 1).toString()
                        targetNum.text =
                            LocalDataDao.INSTANCE.convertUnit(mProfileBean.highTemperatureNum)
                        mProfileBean.highValueF = LocalDataDao.INSTANCE.transUnitCToF(
                            mProfileBean.highTemperatureNum)
                    } else {
                        mProfileBean.highValueF =
                            (mProfileBean.highValueF.toInt() - 1).toString()
                        targetNum.text =
                            mProfileBean.highValueF + LocalDataDao.INSTANCE.getTempUnit()
                        mProfileBean.highTemperatureNum =LocalDataDao.INSTANCE.transUnitFToC(
                            mProfileBean.highValueF)
                    }
                }
                buttonAdd.setOnClickListener {
                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                        if (targetNum.text.toString().split("°").first().toInt() >= 300
                        ) {
                            return@setOnClickListener
                        }
                    } else {
                        if (targetNum.text.toString().split("°").first().toInt() >= 572
                        ) {
                            return@setOnClickListener
                        }
                    }

                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                        mProfileBean.highTemperatureNum =
                            (mProfileBean.highTemperatureNum.toInt() + 1).toString()
                        targetNum.text =
                            LocalDataDao.INSTANCE.convertUnit(mProfileBean.highTemperatureNum)
                        mProfileBean.highValueF = LocalDataDao.INSTANCE.transUnitCToF(
                            mProfileBean.highTemperatureNum)
                    } else {
                        mProfileBean.highValueF =
                            (mProfileBean.highValueF.toInt() + 1).toString()
                        targetNum.text =
                            mProfileBean.highValueF + LocalDataDao.INSTANCE.getTempUnit()
                        mProfileBean.highTemperatureNum =LocalDataDao.INSTANCE.transUnitFToC(
                            mProfileBean.highValueF)
                    }
                }
            }
            TYPE_EDIT_RANGE -> {
                layoutTarget.isVisible = false
                layoutRange.isVisible = true
                layoutCustom.isVisible = false

                if (!(LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_F &&
                            mProfileBean.highValueF.isNotEmpty())
                ) {
                    mProfileBean.highValueF =
                        LocalDataDao.INSTANCE.transUnitCToF(mProfileBean.highTemperatureNum)
                    highestNum.text =
                        LocalDataDao.INSTANCE.convertUnit(mProfileBean.highTemperatureNum)
                } else {
                    highestNum.text = mProfileBean.highValueF + LocalDataDao.INSTANCE.getTempUnit()
                }

                if (!(LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_F &&
                            mProfileBean.highValueF.isNotEmpty())
                ) {
                    mProfileBean.lowValueF =
                        LocalDataDao.INSTANCE.transUnitCToF(mProfileBean.lowTemperatureNum)
                    lowestNum.text =
                        LocalDataDao.INSTANCE.convertUnit(mProfileBean.lowTemperatureNum)
                } else {
                    lowestNum.text = mProfileBean.lowValueF + LocalDataDao.INSTANCE.getTempUnit()
                }

//                mProfileBean.highValueF =
//                    LocalDataDao.INSTANCE.transUnitCToF(mProfileBean.highTemperatureNum)
//                mProfileBean.highValueC = mProfileBean.highTemperatureNum
//                mProfileBean.lowValueF =
//                    LocalDataDao.INSTANCE.transUnitCToF(mProfileBean.lowTemperatureNum)
//                mProfileBean.lowValueC = mProfileBean.lowTemperatureNum
//                highestNum.text = LocalDataDao.INSTANCE.convertUnit(mProfileBean.highTemperatureNum)
//                lowestNum.text = LocalDataDao.INSTANCE.convertUnit(mProfileBean.lowTemperatureNum)


                buttonSubHighest.setOnClickListener {
                    if (lowestNum.text.toString().split("°").first()
                            .toInt() >= highestNum.text.toString()
                            .split("°").first().toInt() - 1
                    ) {
                        return@setOnClickListener
                    }

                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                        mProfileBean.highTemperatureNum =
                            (mProfileBean.highTemperatureNum.toInt() - 1).toString()
                        highestNum.text =
                            LocalDataDao.INSTANCE.convertUnit(mProfileBean.highTemperatureNum)
                        mProfileBean.highValueF = LocalDataDao.INSTANCE.transUnitCToF(
                            mProfileBean.highTemperatureNum)
                    } else {
                        mProfileBean.highValueF =
                            (mProfileBean.highValueF.toInt() - 1).toString()
                        highestNum.text =
                            mProfileBean.highValueF + LocalDataDao.INSTANCE.getTempUnit()
                        mProfileBean.highTemperatureNum =LocalDataDao.INSTANCE.transUnitFToC(
                            mProfileBean.highValueF)
                    }
                }
                buttonAddHighest.setOnClickListener {
                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                        if (highestNum.text.toString().split("°").first().toInt() >= 300
                        ) {
                            return@setOnClickListener
                        }
                    } else {
                        if (highestNum.text.toString().split("°").first().toInt() >= 572
                        ) {
                            return@setOnClickListener
                        }
                    }

                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                        mProfileBean.highTemperatureNum =
                            (mProfileBean.highTemperatureNum.toInt() + 1).toString()
                        highestNum.text =
                            LocalDataDao.INSTANCE.convertUnit(mProfileBean.highTemperatureNum)
                        mProfileBean.highValueF = LocalDataDao.INSTANCE.transUnitCToF(
                            mProfileBean.highTemperatureNum)
                    } else {
                        mProfileBean.highValueF =
                            (mProfileBean.highValueF.toInt() + 1).toString()
                        highestNum.text =
                            mProfileBean.highValueF + LocalDataDao.INSTANCE.getTempUnit()
                        mProfileBean.highTemperatureNum =LocalDataDao.INSTANCE.transUnitFToC(
                            mProfileBean.highValueF)
                    }
                }

                buttonSubLowest.setOnClickListener {
                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                        if (lowestNum.text.toString().split("°").first().toInt() <= 0
                        ) {
                            return@setOnClickListener
                        }
                    } else {
                        if (lowestNum.text.toString().split("°").first().toInt() <= 32
                        ) {
                            return@setOnClickListener
                        }
                    }

                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                        mProfileBean.lowTemperatureNum =
                            (mProfileBean.lowTemperatureNum.toInt() - 1).toString()
                        lowestNum.text =
                            LocalDataDao.INSTANCE.convertUnit(mProfileBean.lowTemperatureNum)
                        mProfileBean.lowValueF = LocalDataDao.INSTANCE.transUnitCToF(
                            mProfileBean.lowTemperatureNum
                        )
                    } else {
                        mProfileBean.lowValueF =
                            (mProfileBean.lowValueF.toInt() - 1).toString()
                        lowestNum.text =
                            mProfileBean.lowValueF + LocalDataDao.INSTANCE.getTempUnit()
                        mProfileBean.lowTemperatureNum = LocalDataDao.INSTANCE.transUnitFToC(
                            mProfileBean.lowValueF
                        )
                    }
                }
                buttonAddLowest.setOnClickListener {
                    if (lowestNum.text.toString().split("°").first()
                            .toInt() >= highestNum.text.toString()
                            .split("°").first().toInt() - 1
                    ) {
                        return@setOnClickListener
                    }

                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                        mProfileBean.lowTemperatureNum =
                            (mProfileBean.lowTemperatureNum.toInt() + 1).toString()
                        lowestNum.text =
                            LocalDataDao.INSTANCE.convertUnit(mProfileBean.lowTemperatureNum)
                        mProfileBean.lowValueF = LocalDataDao.INSTANCE.transUnitCToF(
                            mProfileBean.lowTemperatureNum
                        )
                    } else {
                        mProfileBean.lowValueF =
                            (mProfileBean.lowValueF.toInt() + 1).toString()
                        lowestNum.text =
                            mProfileBean.lowValueF + LocalDataDao.INSTANCE.getTempUnit()
                        mProfileBean.lowTemperatureNum = LocalDataDao.INSTANCE.transUnitFToC(
                            mProfileBean.lowValueF
                        )
                    }
                }
            }
            TYPE_EDIT_CUSTOM -> {
                layoutTarget.isVisible = false
                layoutRange.isVisible = false
                layoutCustom.isVisible = true

                mProfileBean.itemValues.forEach {
                    when {
                        it.itemName.lowercase() == "rare" -> {
                            if (it.itemValue.isNotEmpty()) {
                                contentRare.isVisible = true
//                                rareNum.text = it.itemValue + "°"
//                                rareNum.text = LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                if (!(LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_F &&
                                            it.itemValueF.isNotEmpty())
                                ) {
                                    it.itemValueF =
                                        LocalDataDao.INSTANCE.transUnitCToF(it.itemValue)
                                    rareNum.text = LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                } else {
                                    rareNum.text =
                                        it.itemValueF + LocalDataDao.INSTANCE.getTempUnit()
                                }
                            }
                        }
                        it.itemName.lowercase() == "medium rare" -> {
                            if (it.itemValue.isNotEmpty()) {
//                                midRareNum.text = it.itemValue + "°"
//                                midRareNum.text = LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                contentMidRare.isVisible = true

                                if (!(LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_F &&
                                            it.itemValueF.isNotEmpty())
                                ) {
                                    it.itemValueF =
                                        LocalDataDao.INSTANCE.transUnitCToF(it.itemValue)
                                    midRareNum.text =
                                        LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                } else {
                                    midRareNum.text =
                                        it.itemValueF + LocalDataDao.INSTANCE.getTempUnit()
                                }
                            }
                        }
                        it.itemName.lowercase() == "medium" -> {
                            if (it.itemValue.isNotEmpty()) {
//                                midNum.text = it.itemValue + "°"
//                                midNum.text = LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                contentMid.isVisible = true

                                if (!(LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_F &&
                                            it.itemValueF.isNotEmpty())
                                ) {
                                    it.itemValueF =
                                        LocalDataDao.INSTANCE.transUnitCToF(it.itemValue)
                                    midNum.text = LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                } else {
                                    midNum.text =
                                        it.itemValueF + LocalDataDao.INSTANCE.getTempUnit()
                                }
                            }
                        }
                        it.itemName.lowercase() == "medium well" -> {
                            if (it.itemValue.isNotEmpty()) {
//                                midWellNum.text = it.itemValue + "°"
//                                midWellNum.text = LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                contentMidWell.isVisible = true

                                if (!(LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_F &&
                                            it.itemValueF.isNotEmpty())
                                ) {
                                    it.itemValueF =
                                        LocalDataDao.INSTANCE.transUnitCToF(it.itemValue)
                                    midWellNum.text =
                                        LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                } else {
                                    midWellNum.text =
                                        it.itemValueF + LocalDataDao.INSTANCE.getTempUnit()
                                }
                            }
                        }
                        it.itemName.lowercase() == "well done" -> {
                            if (it.itemValue.isNotEmpty()) {
//                                wellDoneNum.text = it.itemValue + "°"
//                                wellDoneNum.text = LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                contentWellDone.isVisible = true

                                if (!(LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_F &&
                                            it.itemValueF.isNotEmpty())
                                ) {
                                    it.itemValueF =
                                        LocalDataDao.INSTANCE.transUnitCToF(it.itemValue)
                                    wellDoneNum.text =
                                        LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                } else {
                                    wellDoneNum.text =
                                        it.itemValueF + LocalDataDao.INSTANCE.getTempUnit()
                                }
                            }
                        }
                    }
                }

                buttonSubRare.setOnClickListener {
                    updateSubAddItemValue("rare", rareNum, true)
                }
                buttonAddRare.setOnClickListener {
                    updateSubAddItemValue("rare", rareNum, false)
                }

                buttonSubMidRare.setOnClickListener {
                    updateSubAddItemValue("medium rare", midRareNum, true)
                }
                buttonAddMidRare.setOnClickListener {
                    updateSubAddItemValue("medium rare", midRareNum, false)
                }

                buttonSubMid.setOnClickListener {
                    updateSubAddItemValue("medium", midNum, true)
                }
                buttonAddMid.setOnClickListener {
                    updateSubAddItemValue("medium", midNum, false)
                }

                buttonSubMidWell.setOnClickListener {
                    updateSubAddItemValue("medium well", midWellNum, true)
                }
                buttonAddMidWell.setOnClickListener {
                    updateSubAddItemValue("medium well", midWellNum, false)
                }

                buttonSubWellDone.setOnClickListener {
                    updateSubAddItemValue("well done", wellDoneNum, true)
                }
                buttonAddWellDone.setOnClickListener {
                    updateSubAddItemValue("well done", wellDoneNum, false)
                }

            }
            else -> {
                layoutTarget.isVisible = true
                layoutRange.isVisible = false
                layoutCustom.isVisible = false
            }
        }

        viewSave.setOnClickListener {
            LocalDataDao.INSTANCE.updateProfile(mProfileBean)
            mEventViewModel.updateProfile()
            requireActivity().onBackPressed()
        }

        viewDefault.setOnClickListener {
            if (!mProfileBean.isCustomAdd) {
                val defaultProfile = LocalDataDao.INSTANCE.getDefaultProfile(mProfileBean)
                mProfileBean = defaultProfile
                when (currentType) {
                    TYPE_EDIT_TARGET -> {
                        targetNum.text =
                            LocalDataDao.INSTANCE.convertUnit(mProfileBean.highTemperatureNum)
                        mProfileBean.highValueF =
                            LocalDataDao.INSTANCE.transUnitCToF(mProfileBean.highTemperatureNum)
                        mProfileBean.highValueC = mProfileBean.highTemperatureNum
                    }
                    TYPE_EDIT_RANGE -> {
                        mProfileBean.highValueF =
                            LocalDataDao.INSTANCE.transUnitCToF(mProfileBean.highTemperatureNum)
                        mProfileBean.highValueC = mProfileBean.highTemperatureNum
                        mProfileBean.lowValueF =
                            LocalDataDao.INSTANCE.transUnitCToF(mProfileBean.lowTemperatureNum)
                        mProfileBean.lowValueC = mProfileBean.lowTemperatureNum
                        highestNum.text =
                            LocalDataDao.INSTANCE.convertUnit(mProfileBean.highTemperatureNum)
                        lowestNum.text =
                            LocalDataDao.INSTANCE.convertUnit(mProfileBean.lowTemperatureNum)
                    }
                    TYPE_EDIT_CUSTOM -> {
                        mProfileBean.itemValues.forEach {
                            if (it.itemValue.isNotEmpty()) {
                                it.itemValueF = LocalDataDao.INSTANCE.transUnitCToF(it.itemValue)
                            }
                            when {
                                it.itemName.lowercase() == "rare" -> {
                                    if (it.itemValue.isNotEmpty()) {
                                        rareNum.text =
                                            LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                    }
                                }
                                it.itemName.lowercase() == "medium rare" -> {
                                    if (it.itemValue.isNotEmpty()) {
                                        midRareNum.text =
                                            LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                    }
                                }
                                it.itemName.lowercase() == "medium" -> {
                                    if (it.itemValue.isNotEmpty()) {
                                        midNum.text =
                                            LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                    }
                                }
                                it.itemName.lowercase() == "medium well" -> {
                                    if (it.itemValue.isNotEmpty()) {
                                        midWellNum.text =
                                            LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                    }
                                }
                                it.itemName.lowercase() == "well done" -> {
                                    if (it.itemValue.isNotEmpty()) {
                                        wellDoneNum.text =
                                            LocalDataDao.INSTANCE.convertUnit(it.itemValue)
                                    }
                                }
                            }


                        }
                    }
                }
                LocalDataDao.INSTANCE.updateProfile(mProfileBean)
                mEventViewModel.updateProfile()
            } else {
                LocalDataDao.INSTANCE.deleteProfile(mProfileBean)
                mEventViewModel.updateProfile()
                requireActivity().onBackPressed()
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun updateSubAddItemValue(itemName: String, textView: TextView, isSub: Boolean) {
        for (i in mProfileBean.itemValues.indices) {
            if (mProfileBean.itemValues[i].itemName.lowercase() == itemName.lowercase()) {
                if (mProfileBean.itemValues[i].itemValueF.isEmpty() && LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                    mProfileBean.itemValues[i].itemValueF =
                        LocalDataDao.INSTANCE.transUnitCToF(mProfileBean.itemValues[i].itemValue)
                }
                mProfileBean.itemValues[i].itemValueC = mProfileBean.itemValues[i].itemValue
                if (isSub) {
                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                        if (i > 0) {
                            if (mProfileBean.itemValues[i].itemValue.toInt() <= mProfileBean.itemValues[i - 1].itemValue.toInt() + 1) {
                                return
                            }
                        } else {
                            if (mProfileBean.itemValues[i].itemValue.toInt() <= 0) {
                                return
                            }
                        }
                        mProfileBean.itemValues[i].itemValue =
                            (mProfileBean.itemValues[i].itemValue.toInt() - 1).toString()
                        textView.text =
                            mProfileBean.itemValues[i].itemValue + LocalDataDao.TEMP_UNIT_C
                        mProfileBean.itemValues[i].itemValueF =LocalDataDao.INSTANCE.transUnitCToF(
                            mProfileBean.itemValues[i].itemValue
                        )
                    } else {
                        if (i > 0) {
                            if (mProfileBean.itemValues[i].itemValueF.toInt() <= mProfileBean.itemValues[i - 1].itemValueF.toInt() + 1) {
                                return
                            }
                        } else {
                            if (mProfileBean.itemValues[i].itemValueF.toInt() <= 32) {
                                return
                            }
                        }

                        mProfileBean.itemValues[i].itemValueF =
                            (mProfileBean.itemValues[i].itemValueF.toInt() - 1).toString()
                        mProfileBean.itemValues[i].itemValue =
                            LocalDataDao.INSTANCE.transUnitFToC(mProfileBean.itemValues[i].itemValueF)
                        textView.text =
                            mProfileBean.itemValues[i].itemValueF + LocalDataDao.TEMP_UNIT_F
                    }
                } else {
                    if (LocalDataDao.INSTANCE.getTempUnit() == LocalDataDao.TEMP_UNIT_C) {
                        if (i < mProfileBean.itemValues.size - 1) {
                            if (mProfileBean.itemValues[i].itemValue.toInt() >= mProfileBean.itemValues[i + 1].itemValue.toInt() - 1) {
                                return
                            }
                        } else {
                            if (mProfileBean.itemValues[i].itemValue.toInt() >= 300) {
                                return
                            }
                        }

                        mProfileBean.itemValues[i].itemValue =
                            (mProfileBean.itemValues[i].itemValue.toInt() + 1).toString()
                        textView.text =
                            mProfileBean.itemValues[i].itemValue + LocalDataDao.TEMP_UNIT_C
                        mProfileBean.itemValues[i].itemValueF =LocalDataDao.INSTANCE.transUnitCToF(
                            mProfileBean.itemValues[i].itemValue
                        )
                    } else {
                        if (i < mProfileBean.itemValues.size - 1) {
                            if (mProfileBean.itemValues[i].itemValueF.toInt() >= mProfileBean.itemValues[i + 1].itemValueF.toInt() - 1) {
                                return
                            }
                        } else {
                            if (mProfileBean.itemValues[i].itemValueF.toInt() >= 572) {
                                return
                            }
                        }

                        mProfileBean.itemValues[i].itemValueF =
                            (mProfileBean.itemValues[i].itemValueF.toInt() + 1).toString()
                        mProfileBean.itemValues[i].itemValue =
                            LocalDataDao.INSTANCE.transUnitFToC(mProfileBean.itemValues[i].itemValueF)
                        textView.text =
                            mProfileBean.itemValues[i].itemValueF + LocalDataDao.TEMP_UNIT_F
                    }
                }
            }
        }

    }
}